package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.OrderInfo;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.Setmeal;
import com.itheima.utils.DateUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private SetmealDao setmealDao;

    @Autowired
    private OrderDao orderDao;
    @Override
    @Transactional
    public Result toOrder(Map map) throws Exception {
        //1.根据预约时间判断是否设置预约
        Date date = DateUtils.parseString2Date((String) map.get("orderDate"));
        OrderSetting orderSetting = orderSettingDao.findOrderSettingByOrderDate(date);
        if (orderSetting == null){
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }
        //2.判断是否预约已满
        if (orderSetting.getReservations() >= orderSetting.getNumber()){
            return new Result(false,MessageConstant.ORDER_FULL);
        }
        //3.判断该用户是否存在，不存在进行创建
        Integer setmealId = Integer.parseInt((String)map.get("setmealId"));
        Member member = memberDao.findByPhoneNumber((String) map.get("telephone"));
        if (member == null){
            member = new Member();
            member.setName((String) map.get("name"));
            member.setPhoneNumber((String) map.get("telephone"));
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            member.setRegTime(new Date());
            memberDao.add(member);
        }else {
            //4.根据套餐的id来判断套餐是否存在
            Setmeal setmeal = setmealDao.findById(setmealId);
            if (setmeal == null){
                return new Result(false,"此套餐不存在，请重新进行预约");
            }
        }
        //5.(1)套餐存在 则根据会员id 套餐id 已预约时间 来判断是否重复预约
        Order order = new Order(member.getId(),date,setmealId);
        Order order1 = orderDao.findOrder(order);
        if (order1 != null){
            return new Result(false,MessageConstant.HAS_ORDERED);
        }
        //5.(2)没有重复预约 添加预约订单 并返回id
        Order order2 = null;
        try {
            order2 = new Order(member.getId(),date,Order.ORDERTYPE_WEIXIN,Order.ORDERSTATUS_NO,setmealId);
            orderDao.add(order2);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDER_FAIL);
        }
        //6.预约日期已预约人数+1
        orderSettingDao.updateReservation(orderSetting);
        return new Result(true,MessageConstant.ORDER_SUCCESS,order2.getId());
    }

    @Override
    public OrderInfo findById(Integer id) {
        return orderDao.findById(id);
    }

    @Override
    public Map<String, Integer> findNumByDate() {
        LocalDate localDate = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd");
        //今天日期
        String today = localDate.format(formatter);
        //本周起始日期
        String thisWeekStartDay = localDate.with(DayOfWeek.MONDAY).format(formatter);
        //本月开始日期
        String thisMonthStartDay = LocalDate.of(localDate.getYear(),localDate.getMonth(),1).format(formatter);
        Integer todayNoStatusNums = orderDao.findNumsByDateAndOrderStatus(today, null,null);
        Integer todayStatusNums = orderDao.findNumsByDateAndOrderStatus(today, null, Order.ORDERSTATUS_YES);
        Integer thisWeekNoStatusNums = orderDao.findNumsByDateAndOrderStatus(null, thisWeekStartDay, null);
        Integer thisWeekStatusNums = orderDao.findNumsByDateAndOrderStatus(null, thisWeekStartDay, Order.ORDERSTATUS_YES);
        Integer thisMonthNoStatusNums = orderDao.findNumsByDateAndOrderStatus(null,thisMonthStartDay,null);
        Integer thisMonthStatusNums = orderDao.findNumsByDateAndOrderStatus(null,thisMonthStartDay,Order.ORDERSTATUS_YES);
        Map<String,Integer> map = new HashMap<>();
        map.put("todayOrderNumber",todayNoStatusNums);
        map.put("todayVisitsNumber",todayStatusNums);
        map.put("thisWeekOrderNumber",thisWeekNoStatusNums);
        map.put("thisWeekVisitsNumber",thisWeekStatusNums);
        map.put("thisMonthOrderNumber",thisMonthNoStatusNums);
        map.put("thisMonthVisitsNumber",thisMonthStatusNums);
        return map;
    }
}
